@echo off
echo 🚀 Starting Molecule Scope Application (Local Development)...

REM 设置本地开发环境变量
set DEPLOYMENT_ENV=development
set SERVER_URL=http://localhost
set BACKEND_PORT=8001
set FRONTEND_PORT=8002

REM 检查Python
python --version >nul 2>&1
if errorlevel 1 (
    python3 --version >nul 2>&1
    if errorlevel 1 (
        echo ❌ Python is not installed. Please install Python 3.8+
        pause
        exit /b 1
    )
)

REM 检查Node.js
node --version >nul 2>&1
if errorlevel 1 (
    echo ❌ Node.js is not installed. Please install Node.js 16+
    pause
    exit /b 1
)

REM 设置开发环境变量
set VITE_API_BASE_URL=%SERVER_URL%:%BACKEND_PORT%

REM 检查端口是否被占用
echo 🔍 Checking if port %BACKEND_PORT% is available...
netstat -ano | findstr :%BACKEND_PORT% >nul
if not errorlevel 1 (
    echo ⚠️  Port %BACKEND_PORT% is already in use. Stopping existing processes...
    for /f "tokens=5" %%a in ('netstat -ano ^| findstr :%BACKEND_PORT% ^| findstr LISTENING') do (
        taskkill /f /pid %%a
    )
    timeout /t 2 /nobreak >nul
)

echo 📦 Installing Python dependencies...
pip install -r requirements.txt
if errorlevel 1 (
    echo ❌ Failed to install Python dependencies
    pause
    exit /b 1
)

echo 📦 Installing Node.js dependencies...
cd client
npm install
if errorlevel 1 (
    echo ❌ Failed to install Node.js dependencies
    pause
    exit /b 1
)
cd ..

echo 📦 Starting FastAPI backend server on port %BACKEND_PORT%...
start "Molecule Scope Backend" cmd /k "python -m uvicorn main:app --reload --host 0.0.0.0 --port %BACKEND_PORT%"

REM 等待后端启动（更长的等待时间）
echo ⏳ Waiting for backend to start (10 seconds)...
timeout /t 10 /nobreak >nul

REM 使用PowerShell来检查后端是否成功启动（替代curl）
echo 🔍 Checking if backend server is running...
powershell -Command "$response = Invoke-WebRequest -Uri 'http://localhost:%BACKEND_PORT%/health' -Method Get -ErrorAction SilentlyContinue; if ($response.StatusCode -eq 200) { exit 0 } else { exit 1 }"
if errorlevel 1 (
    echo ❌ Backend server failed to start on port %BACKEND_PORT%
    echo Please check the backend window for error messages.
    echo.
    echo Trying to start without health check...
    goto start_frontend
)

:start_frontend
REM 检查前端端口是否被占用
echo 🔍 Checking if port %FRONTEND_PORT% is available...
netstat -ano | findstr :%FRONTEND_PORT% >nul
if not errorlevel 1 (
    echo ⚠️  Port %FRONTEND_PORT% is already in use. Stopping existing processes...
    for /f "tokens=5" %%a in ('netstat -ano ^| findstr :%FRONTEND_PORT% ^| findstr LISTENING') do (
        taskkill /f /pid %%a
    )
    timeout /t 2 /nobreak >nul
)

echo 🌐 Starting Vue.js frontend server on port %FRONTEND_PORT%...
cd client
start "Molecule Scope Frontend" cmd /k "set VITE_API_BASE_URL=%SERVER_URL%:%BACKEND_PORT% && npm run dev -- --port %FRONTEND_PORT%"
cd ..

echo.
echo ✅ Application started successfully!
echo 📊 Backend API: %SERVER_URL%:%BACKEND_PORT%
echo 🎨 Frontend App: %SERVER_URL%:%FRONTEND_PORT%
echo 📚 API Docs: %SERVER_URL%:%BACKEND_PORT%/docs
echo.
echo Both servers are running in separate windows.
echo Close the windows to stop the servers.
echo.
echo If you see any errors, please check the respective windows.
pause